package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: 李旭
 * @Time: 15:32
 * @Description:
 *
 * 后台管理中心
 *    VUE  -- 跨域  -- 网关  --- 路由失败 跨域  -- 商品微服务
 *
 */
//@CrossOrigin  必须去掉
@RestController
@RequestMapping("admin/product")
public class ManagerController {


    @Autowired //全家桶
    private ManagerService managerService;
    //查询一级分类集合
    @GetMapping("getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List =  managerService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //查询二级分类 根据一级分类的ID
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable Long category1Id){

        List<BaseCategory2> baseCategory2List =  managerService.getCategory2(category1Id);
        return Result.ok(baseCategory2List);
    }
    //查询三级分类 根据一级分类的ID
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable Long category2Id){

        List<BaseCategory3> baseCategory3List =  managerService.getCategory3(category2Id);
        return Result.ok(baseCategory3List);
    }
    //#根据一二三级分类的ID  层级查询 平台属性及属性值表
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable Long category1Id,
            @PathVariable Long category2Id,
            @PathVariable Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfoList =  managerService.
                attrInfoList(category1Id,category2Id,category3Id);
        return Result.ok(baseAttrInfoList);
    }

    //添加平台属性及值
    @PostMapping("saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        managerService.saveAttrInfo(baseAttrInfo);

        return Result.ok();
    }


    //查询品牌列表 （分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable Integer page,@PathVariable Integer limit){
        //分页对象  接口
        IPage iPage = managerService.baseTrademark(page,limit);
        return Result.ok(iPage);
    }
    //查询SPU商品列表  （分页）
    @GetMapping("/{page}/{limit}")
    public Result spuInfoList (@PathVariable Integer page,@PathVariable Integer limit
    ,Long category3Id){
        //分页对象  接口
        IPage iPage = managerService.spuInfoList(page,limit,category3Id);
        return Result.ok(iPage);
    }

    //查询品牌列表
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }
    //查询销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = managerService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //保存spu
    @PostMapping("saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }
    //根据商品ID spuId 查询所有图片 显示在Sku添加页面 目标：为了让管理员来选择
   @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable Long spuId){

        List<SpuImage>  spuImageList = managerService.spuImageList(spuId);
       return Result.ok(spuImageList);
   }

   //根据SpuId查询销售属性及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //添加库存
    @PostMapping("saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){

        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //SKU分页查询
    @GetMapping("/list/{page}/{limit}")
    public Result skuListPage(@PathVariable Integer page,@PathVariable Integer limit){
        IPage iPage = managerService.skuListPage(page,limit);
        return Result.ok(iPage);
    }

    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){

        managerService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
